package app

import (
	"chatgpt/ai/openai"
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/plugin/otherlogin"
	"github.com/gin-gonic/gin"
	"github.com/slclub/go-tips"
	"strconv"
	"strings"
	"time"
)

func GetUserWxOpenID(ctx *gin.Context) {
	wxCode := ctx.Query("wxcode")
	if wxCode == "" {
		helper.Echo(ctx, int(code.REQ_PARAM_ERR), "")
		return
	}

	result, _ := otherlogin.GetWechatOpenID(wxCode)
	helper.Echo(ctx, int(code.SUCCESS), result)
}

func GetRecordList(ctx *gin.Context) {
	page := ctx.Query("page")
	pageSize := ctx.Query("pageSize")
	startTime := ctx.Query("startTime")
	endTime := ctx.Query("endTime")

	if _, err := strconv.ParseFloat(page, 64); err != nil {
		page = tips.String(1)
	} else {
		if page == "" || tips.Int(page) < 1 {
			page = tips.String(1)
		}
	}

	if _, err := strconv.ParseFloat(pageSize, 64); err != nil {
		pageSize = tips.String(10)
	} else {
		if pageSize == "" || tips.Int(pageSize) < 1 {
			pageSize = tips.String(10)
		} else {
			if tips.Int(pageSize) > 100 {
				pageSize = tips.String(100)
			}
		}
	}

	loc, _ := time.LoadLocation("Asia/Shanghai")
	timeFormat := "2006-01-02 15:04:05"
	if startTime == "" || endTime == "" {
		//fmt.Println("没有startTime或者endTime")
		startTime = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-30, 00, 00, 00, 00, loc).Format(timeFormat)
		endTime = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 23, 59, 59, 59, loc).Format(timeFormat)
	} else {
		startTimeArr := strings.Split(startTime, "-")
		startTimeY := startTimeArr[0]
		startTimeM := startTimeArr[1]
		startTimeD := startTimeArr[2]

		if len(startTimeY) != 4 {
			startTimeY = tips.String(2023)
		}

		if len(startTimeM) == 1 {
			startTimeM = "0" + startTimeM
		}

		if len(startTimeD) == 1 {
			startTimeD = "0" + startTimeD
		}

		endTimeArr := strings.Split(endTime, "-")
		endTimeY := endTimeArr[0]
		endTimeM := endTimeArr[1]
		endTimeD := endTimeArr[2]

		if len(endTimeY) != 4 {
			endTimeY = tips.String(2023)
		}

		if len(endTimeM) == 1 {
			endTimeM = "0" + endTimeM
		}

		if len(endTimeD) == 1 {
			endTimeD = "0" + endTimeD
		}

		startTime = startTimeY + "-" + startTimeM + "-" + startTimeD + " 00:00:00"
		endTime = endTimeY + "-" + endTimeM + "-" + endTimeD + " 23:59:59"
	}

	parseStartTime, startTimeErr := time.ParseInLocation(timeFormat, startTime, loc)
	parseEndTime, endTimeErr := time.ParseInLocation(timeFormat, endTime, loc)

	if startTimeErr != nil || endTimeErr != nil {
		//fmt.Println("开始时间或者结束时间格式错误")
		parseStartTime = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-30, 00, 00, 00, 00, loc)
		parseEndTime = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 23, 59, 59, 59, loc)
	}

	parseStartTimestamp := parseStartTime.Unix()
	parseEndTimestamp := parseEndTime.Unix()

	if parseEndTimestamp < parseStartTimestamp {
		//fmt.Println("开始时间比结束时间大")
		parseStartTime = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-30, 00, 00, 00, 00, loc)
		parseEndTime = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 23, 59, 59, 59, loc)

		parseStartTimestamp = parseStartTime.Unix()
		parseEndTimestamp = parseEndTime.Unix()
	}

	diffDay := tips.Int(parseEndTime.Sub(parseStartTime).Hours()/24 + 1)

	gptRecord := &openai.AppGPT{}
	list, err := gptRecord.GetRecordList(tips.Int(page), tips.Int(pageSize), parseStartTimestamp, parseEndTimestamp)
	totalDetail, err := gptRecord.GetRecordTotalDetail(parseStartTime.Format(timeFormat), parseStartTimestamp, parseEndTime.Format(timeFormat), parseEndTimestamp, diffDay)

	if err != nil {
		return
	}

	total, err := gptRecord.GetRecordTotal()
	if err != nil {
	}

	data := map[string]any{
		"total":        total,
		"total_detail": totalDetail,
		"list":         list,
		"page":         tips.Any2Int64(page),
		"pageSize":     tips.Any2Int64(pageSize),
	}

	helper.Echo(ctx, int(code.SUCCESS), data)
}
